<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  
  <title>url | H5教案</title>

  
  <meta name="author" content="John Doe">
  

  
  <meta name="description" content="1、url新建URL对象
12const url=require(&amp;quot;url&amp;quot;);var myUrl=new URL(&amp;quot;http://10.9.63.202:3005?name=xie&amp;quot;);
1.1 parseurl.parse(urlString[, pars">
  

  
  
  <meta name="keywords" content="">
  

  <meta id="viewport" name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">

  <meta property="og:title" content="url"/>

  <meta property="og:site_name" content="H5教案"/>

  
  <meta property="og:image" content="/ericxie-book/favicon.ico"/>
  

  <link href="/ericxie-book/favicon.ico" rel="icon">
  <link rel="alternate" href="/ericxie-book/atom.xml" title="H5教案" type="application/atom+xml">
  <link rel="stylesheet" href="/ericxie-book/css/style.css" media="screen" type="text/css">
<meta name="generator" content="Hexo 5.3.0"></head>


<body>
<div class="blog">
  <div class="content">

    <header>
  <div class="site-branding">
    <h1 class="site-title">
      <a href="/ericxie-book/">H5教案</a>
    </h1>
    <p class="site-description"></p>
  </div>
  <nav class="site-navigation">
    <ul>
      
    </ul>
  </nav>
</header>

    <main class="site-main posts-loop">
    <article>

  
    
    <h3 class="article-title"><span>url</span></h3>
    
  

  <div class="article-top-meta">
    <span class="posted-on">
      <a href="/ericxie-book/2021/01/25/url/" rel="bookmark">
        <time class="entry-date published" datetime="2021-01-25T07:59:47.000Z">
          2021-01-25
        </time>
      </a>
    </span>
  </div>


  

  <div class="article-content">
    <div class="entry">
      
        <h2 id="1、url"><a href="#1、url" class="headerlink" title="1、url"></a>1、url</h2><p>新建URL对象</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">const url=require(<span class="string">&quot;url&quot;</span>);</span><br><span class="line">var myUrl=new URL(<span class="string">&quot;http://10.9.63.202:3005?name=xie&quot;</span>);</span><br></pre></td></tr></table></figure>
<h3 id="1-1-parse"><a href="#1-1-parse" class="headerlink" title="1.1 parse"></a>1.1 parse</h3><p>url.parse(urlString[, parseQueryString[, slashesDenoteHost]])</p>
<img src="/ericxie-book/2021/01/25/url/url.jpg" class="" title="url图片">

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">const url = require(<span class="string">&#x27;url&#x27;</span>)</span><br><span class="line">const urlString = <span class="string">&#x27;https://www.baidu.com:443/ad/index.html?id=8&amp;name=mouse#tag=110&#x27;</span></span><br><span class="line">const parsedStr = url.parse(urlString)</span><br><span class="line">console.log(parsedStr)</span><br></pre></td></tr></table></figure>
<h3 id="1-2-format"><a href="#1-2-format" class="headerlink" title="1.2 format"></a>1.2 format</h3><p>url.format(urlObject)</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">const url = require(<span class="string">&#x27;url&#x27;</span>)</span><br><span class="line">const urlObject = &#123;</span><br><span class="line">    protocol: <span class="string">&#x27;https:&#x27;</span>,</span><br><span class="line">    slashes: <span class="literal">true</span>,</span><br><span class="line">    auth: null,</span><br><span class="line">    host: <span class="string">&#x27;www.baidu.com:443&#x27;</span>,</span><br><span class="line">    port: <span class="string">&#x27;443&#x27;</span>,</span><br><span class="line">    hostname: <span class="string">&#x27;www.baidu.com&#x27;</span>,</span><br><span class="line">    <span class="built_in">hash</span>: <span class="string">&#x27;#tag=110&#x27;</span>,</span><br><span class="line">    search: <span class="string">&#x27;?id=8&amp;name=mouse&#x27;</span>,</span><br><span class="line">    query: &#123; </span><br><span class="line">        id: <span class="string">&#x27;8&#x27;</span>, name: <span class="string">&#x27;mouse&#x27;</span> </span><br><span class="line">        &#125;,</span><br><span class="line">    pathname: <span class="string">&#x27;/ad/index.html&#x27;</span>,</span><br><span class="line">    path: <span class="string">&#x27;/ad/index.html?id=8&amp;name=mouse&#x27;</span>,</span><br><span class="line">    href: <span class="string">&#x27;https://www.baidu.com:443/ad/index.html?id=8&amp;name=mouse#tag=110&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">const parsedObj = url.format(urlObject)</span><br><span class="line">console.log(parsedObj)</span><br></pre></td></tr></table></figure>
<h3 id="1-3-resolve"><a href="#1-3-resolve" class="headerlink" title="1.3 resolve"></a>1.3 resolve</h3><p>url.resolve(from, to)</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">const url = require(<span class="string">&#x27;url&#x27;</span>)</span><br><span class="line">var a = url.resolve(<span class="string">&#x27;/one/two/three&#x27;</span>, <span class="string">&#x27;four&#x27;</span>)</span><br><span class="line">var b = url.resolve(<span class="string">&#x27;http://example.com/&#x27;</span>, <span class="string">&#x27;/one&#x27;</span>)</span><br><span class="line">var c = url.resolve(<span class="string">&#x27;http://example.com/one&#x27;</span>, <span class="string">&#x27;/two&#x27;</span>)</span><br><span class="line">console.log(a + <span class="string">&quot;,&quot;</span> + b + <span class="string">&quot;,&quot;</span> + c)</span><br></pre></td></tr></table></figure>
<h3 id="1-4-log4js插件"><a href="#1-4-log4js插件" class="headerlink" title="1.4 log4js插件"></a>1.4 log4js插件</h3><p>log4js是一个日志处理文件</p>
<p>安装</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm i log4js -D</span><br></pre></td></tr></table></figure>
<p>使用方式</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">const log4js = require(<span class="string">&quot;log4js&quot;</span>);</span><br><span class="line">log4js.configure(&#123;</span><br><span class="line">  appenders: &#123; cheese: &#123; <span class="built_in">type</span>: <span class="string">&quot;file&quot;</span>, filename: <span class="string">&quot;cheese.log&quot;</span> &#125; &#125;,</span><br><span class="line">  categories: &#123; default: &#123; appenders: [<span class="string">&quot;cheese&quot;</span>], level: <span class="string">&quot;error&quot;</span> &#125; &#125;</span><br><span class="line">&#125;);</span><br><span class="line">const logger = log4js.getLogger(<span class="string">&quot;cheese&quot;</span>);</span><br><span class="line">logger.level=<span class="string">&quot;debug&quot;</span>;</span><br><span class="line"></span><br><span class="line">var str=<span class="string">&quot;http://www.163.com/news?a=3&quot;</span>;</span><br><span class="line">logger.debug(url.parse(str));</span><br></pre></td></tr></table></figure>
<h3 id="1-5-URLSearchParams"><a href="#1-5-URLSearchParams" class="headerlink" title="1.5 URLSearchParams"></a>1.5 URLSearchParams</h3><p>该类是用来搜索地址中的参数，并且获取到指定的参数值</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">var str=<span class="string">&quot;http://www.163.com/news/?a=3&amp;b=4&quot;</span>;</span><br><span class="line">var search=url.parse(str).search;</span><br><span class="line">var urlParams=new URLSearchParams(search);</span><br><span class="line">var a=urlParams.get(<span class="string">&quot;a&quot;</span>);</span><br><span class="line">var b=urlParams.get(<span class="string">&quot;b&quot;</span>);</span><br></pre></td></tr></table></figure>
<h3 id="Crypto"><a href="#Crypto" class="headerlink" title="Crypto"></a>Crypto</h3><p>加密</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">const crypto = require(<span class="string">&#x27;crypto&#x27;</span>)</span><br><span class="line">const secret = <span class="string">&#x27;abcdefg&#x27;</span></span><br><span class="line">const <span class="built_in">hash</span> = crypto.createHmac(<span class="string">&#x27;sha256&#x27;</span>, secret)</span><br><span class="line">                   .update(<span class="string">&#x27;I love you&#x27;</span>)</span><br><span class="line">                   .digest(<span class="string">&#x27;hex&#x27;</span>)</span><br><span class="line">console.log(<span class="built_in">hash</span>);</span><br></pre></td></tr></table></figure>
      
    </div>

  </div>

  <div class="article-footer">
    <div class="article-meta pull-left">

    

    

    </div>

    
  </div>
</article>

  






    </main>

    <footer class="site-footer">
  <p class="site-info">
    Proudly powered by <a href="https://hexo.io/" target="_blank">Hexo</a> and
    Theme by <a href="https://github.com/CodeDaraW/Hacker" target="_blank">Hacker</a>
    </br>
    
    &copy; 2021 John Doe
    
  </p>
</footer>
    
    
  </div>
</div>
</body>
</html>